'use strict';
const { billTable, ObjectId } = require('../models/database_tables');

class BillService  {
  
  async getList(user_id) {
    try {
      const result = await billTable.where({user_id}).find()
      return result;
    } catch (err) {
      console.log(err);
      return null;
    }
  }
  async add(params) {
    try {
      const result = await billTable.save(params)
      return result;
    } catch (err) {
      console.log(err);
      return null;
    }
  }

  async delete(bill_id) {
    try {
      let result = await billTable.where({ _id: ObjectId(bill_id) }).findOne()
      if(!result) return null
      result = await billTable.delete(result)
      return result;
    } catch (err) {
      console.log(err);
      return null;
    }
  }

  async detail(bill_id) {
    try {
      let result = await billTable.where({ _id: ObjectId(bill_id) }).findOne()
      return result;
    } catch (err) {
      console.log(err);
      return null;
    }
  }
  async edit(params) {
    try {
      const result = await billTable.save(params);
      return result;
    } catch (err) {
      console.log(err);
      return null;
    }
  }
}

module.exports = new BillService();
